<template>
		<el-card class="layout-query">
		<el-form ref="queryRef" :inline="true" :model="state.queryForm" @keyup.enter="getDataList()">
			<el-form-item>
				<el-input v-model="state.queryForm.orderId" placeholder="请输入订单号" clearable />
			</el-form-item>
			<el-form-item>
				<el-button icon="Search" type="primary" @click="getDataList()">查询</el-button>
			</el-form-item>
			<el-form-item>
				<el-button icon="RefreshRight" @click="reset(queryRef)">重置</el-button>
			</el-form-item>
		</el-form>
	</el-card>

	<el-card>
		<el-space>
			<!-- <el-space>
				<el-button icon="Plus" type="primary" @click="addOrUpdateHandle()">新增</el-button>
			</el-space> -->
			<el-space>
				<el-button plain icon="Download" @click="downloadHandle('/ss/order/export')">导出</el-button>
			</el-space>
			<el-space>
				<el-button icon="Delete" plain type="danger" @click="deleteBatchHandle()">批量删除</el-button>
			</el-space>
		</el-space>
		<el-table v-loading="state.dataListLoading" :data="state.dataList" border class="layout-table" show-overflow-tooltip @selection-change="selectionChangeHandle" >
			<el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
			<el-table-column prop="orderId" label="订单号" header-align="center" align="center"></el-table-column>
			<el-table-column prop="deviceId" label="设备编号" header-align="center" align="center"></el-table-column>
			<el-table-column prop="scenic" label="景区" header-align="center" align="center"></el-table-column>
			<el-table-column prop="deviceName" label="设备名称" header-align="center" align="center"></el-table-column>
			<el-table-column prop="phone" label="手机号" header-align="center" align="center"></el-table-column>
			<el-table-column prop="orderTransactionId" label="交易号" header-align="center" align="center"></el-table-column>
			<el-table-column prop="orderStatus" label="订单状态" header-align="center" align="center">
				<template #default="scope">
        <span>{{ formatOrderStatus(scope.row.orderStatus) }}</span>
    </template>
			</el-table-column>
			<el-table-column prop="orderStartTime" label="骑行开始时间" header-align="center" align="center"></el-table-column>
			<el-table-column prop="orderEndTime" label="骑行结束时间" header-align="center" align="center"></el-table-column>
			<el-table-column prop="orderTotalCost" label="订单总金额（元）" header-align="center" align="center"></el-table-column>
			<el-table-column prop="orderRealCost" label="订单实付金额（元）" header-align="center" align="center">
				<template #default="scope">
        <span>{{ scope.row.orderPayStatus == 1 ? scope.row.orderRealCost : "暂无" }}</span>
    </template>
			</el-table-column>
			<el-table-column prop="orderRefundCost" label="订单退款金额（元）" header-align="center" align="center"></el-table-column>
			<el-table-column prop="orderDispatchOrder" label="调度费订单" header-align="center" align="center"></el-table-column>
			<el-table-column prop="orderPayStatus" label="支付状态" header-align="center" align="center">
				<template #default="scope">
        <span>{{ formatPayStatus(scope.row.orderPayStatus) }}</span>
    </template>

			</el-table-column>
			<el-table-column label="操作" fixed="right" header-align="center" align="center" width="150">
				<template #default="scope">
					<el-button type="primary" link @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
					<el-button type="primary" link @click="closeOrderHandle(scope.row.orderId,scope.row.wxOpenId)">关闭</el-button>
				</template>
			</el-table-column>
		</el-table>
		<el-pagination
			:current-page="state.page"
			:page-sizes="state.pageSizes"
			:page-size="state.limit"
			:total="state.total"
			layout="total, sizes, prev, pager, next, jumper"
			@size-change="sizeChangeHandle"
			@current-change="currentChangeHandle"
		>
		</el-pagination>

		<!-- 弹窗, 新增 / 修改 -->
		<add-or-update v-if="addOrUpdateVisible" ref="addOrUpdateRef" v-model:visible="addOrUpdateVisible" @refreshDataList="getDataList"></add-or-update>
	</el-card>
</template>

<script setup lang="ts" name="OrderOrderIndex">
	import {useCrud} from '@/hooks'
	import {reactive, nextTick, ref} from 'vue'
import { ElMessage,ElMessageBox } from 'element-plus/es'
	import {IHooksOptions} from '@/hooks/interface'
	import AddOrUpdate from './add-or-update.vue'
	import { closeUserOrderSubmitApi, useOrderOrderSubmitApi } from '@/api/order/order'

	
	const state: IHooksOptions = reactive({
	dataListUrl: '/ss/order/page',
	deleteUrl: '/ss/order',
	queryForm: {
		orderId: ''
	}
})

const queryRef = ref()
const addOrUpdateVisible = ref(false)
const addOrUpdateRef = ref()
const dataForm = reactive({
	id: '',
	orderId: '',
	deviceId: '',
	deviceName: '',
	wxOpenId: '',
	orderStatus: '',
	orderStartTime: '',
	orderEndTime: '',
	orderTotalCost: '',
	orderRealCost: '',
	orderRefundCost: '',
	orderDispatchOrder: '',
	orderPayStatus: '',
	creator: '',
	createTime: '',
	updater: '',
	updateTime: '',
	deleted: ''
})


const addOrUpdateHandle = (id?: number) => {
	addOrUpdateVisible.value = true
	nextTick(() => addOrUpdateRef.value.init(id))
}

const formatOrderStatus = (status: number | string) => {
    switch (status) {
        case 0:
        case '0':
            return '未完成'
        case 1:
        case '1':
            return '已完成'
        default:
            return '未完成'
    }
}
const formatPayStatus = (status: number | string) => {
    switch (status) {
        case 0:
        case '0':
            return '未支付'
        case 1:
        case '1':
            return '已支付'
        default:
            return '未支付'
    }
}


const closeOrderHandle = (orderId: String,wxOpenId: String) => {
    // 使用 ElMessageBox 添加确认操作
    ElMessageBox.confirm(
        '确定要关闭此订单吗?', 
        '提示', 
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        }
    ).then(() => {
		console.log(orderId,wxOpenId)
        // 用户点击确定
		const data = {
			orderId:orderId,
			openId:wxOpenId
		}
        closeUserOrderSubmitApi(data).then(res => {
			getDataList()
            ElMessage.success('关闭订单成功')
        }).catch(err => {
            ElMessage.error('关闭订单失败: ' + err.message)
        })
    }).catch(() => {
        // 用户点击取消
        ElMessage.info('已取消关闭订单')
    })
}


// 表单提交
const submitHandle = () => {
	useOrderOrderSubmitApi(dataForm).then(() => {
			ElMessage.success({
				message: '操作成功',
				duration: 500,
				onClose: () => {
					getDataList()
				}
			})
		})
	}


const { getDataList, selectionChangeHandle, sizeChangeHandle, currentChangeHandle, deleteBatchHandle, downloadHandle, reset } = useCrud(state)
</script>
